package com.hzit.util;


import cn.hutool.core.bean.BeanUtil;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.Classes;
import com.hzit.entity.Student;
import com.hzit.mapper.ClassesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class Conver {

	@Autowired
	private ClassesMapper classesMapper;

	//1. 将enitity实体类转换为DTO对象
	public List<StudentDTO> entity2DTO(List<Student> records) {
		return records.stream().map(m -> {
			StudentDTO dto = new StudentDTO();
			BeanUtil.copyProperties(m,dto,true);
			Classes classes = classesMapper.selectById(m.getCid());
			dto.setCname(classes.getCname());
			return dto;
		}).collect(Collectors.toList());
	}
	//2. 将dto转换为entity
	public Student dto2Entity(StudentDTO dto) {
		Student student = new Student();
		BeanUtil.copyProperties(dto,student,true);
		return student;
	}
}
